﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace DataService.Helper
{
    public class DataHelper
    {
        public static List<T> TableToList<T>(DataTable table)
            where T : new()
        {
            List<T> dataList = new List<T>();
            T data = new T();
            foreach (DataRow item in table.Rows)
            {
                int i = 0;
                foreach (DataColumn column in table.Columns)
                {
                    if (item.ItemArray[i] != DBNull.Value)
                    {
                        if (item.ItemArray[i].GetType() == typeof(System.UInt64))
                        {
                            data.GetType().GetProperties().FirstOrDefault(m => m.Name == column.ColumnName)?.SetValue(data, long.Parse(item.ItemArray[i].ToString()));
                        }
                        else
                        {
                            data.GetType().GetProperties().FirstOrDefault(m => m.Name == column.ColumnName)?.SetValue(data, item.ItemArray[i]);
                        }
                    }
                    i++;
                }
                dataList.Add(data);
            }
            return dataList;
        }

        public static T DataRowToModel<T>(DataColumnCollection columns, DataRow row)
            where T : new()
        {
            T data = new T();
            int i = 0;
            foreach (DataColumn column in columns)
            {
                if (row.ItemArray[i] != DBNull.Value)
                {
                    if (row.ItemArray[i].GetType() == typeof(System.UInt64))
                    {
                        data.GetType().GetProperties().FirstOrDefault(m => m.Name == column.ColumnName)?.SetValue(data, long.Parse(row.ItemArray[i].ToString()));
                    }
                    else
                    {
                        data.GetType().GetProperties().FirstOrDefault(m => m.Name == column.ColumnName)?.SetValue(data, row.ItemArray[i]);
                    }
                }

                i++;
            }
            return data;
        }

        public static DataTable ToDataTable<T>(IEnumerable<T> collection)
        {
            var props = typeof(T).GetProperties();
            var dt = new DataTable();
            dt.Columns.AddRange(props.Select(p => new DataColumn(p.Name, p.PropertyType)).ToArray());
            if (collection.Count() > 0)
            {
                for (int i = 0; i < collection.Count(); i++)
                {
                    ArrayList tempList = new ArrayList();
                    foreach (PropertyInfo pi in props)
                    {
                        object obj = pi.GetValue(collection.ElementAt(i), null);
                        tempList.Add(obj);
                    }
                    object[] array = tempList.ToArray();
                    dt.LoadDataRow(array, true);
                }
            }
            return dt;
        }

        public static DataTable ToDataTable<T>(T data)
        {
            var props = typeof(T).GetProperties();
            var dt = new DataTable();
            dt.Columns.AddRange(props.Select(p => new DataColumn(p.Name, p.PropertyType)).ToArray());
            ArrayList tempList = new ArrayList();
            foreach (PropertyInfo pi in props)
            {
                object obj = pi.GetValue(data, null);
                tempList.Add(obj);
            }
            object[] array = tempList.ToArray();
            dt.LoadDataRow(array, true);
            return dt;
        }
    }
}
